home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / Devcon_Extras / Tech_Articles / 8520_Timing < prev    next >
Encoding:
Text File  |  1992-08-27  |  4.0 KB  |  129 lines

  1.  
  2. How To Waste Time
  3.  
  4. * Copyright 1988 Commodore-Amiga, Inc.
  5. *  
  6. * Executables based on this information may be used in software
  7. * for Commodore Amiga computers.  All other rights reserved.
  8. * This information is provided "as is"; no warranties are made.  All 
  9. * use is at your own risk. No liability or responsibility is assumed.
  10.  
  11.  
  12. The worst way to insert a delay in an Amiga program is this:
  13.  
  14.          move.w #2000,d0
  15.     loop dbra   loop,d0
  16.  
  17. This loop, or anything like it is *unacceptable*.
  18.  
  19. If running under the multitasking operating system, the
  20. timer.device can provide delay that still lets other tasks run. 
  21. If taking over the machine, a loop like this is far better:
  22.  
  23.     loop btst.b #0,$bfed01
  24.          beq.s  loop
  25.  
  26. This uses one of the high speed timer chips.  As included example
  27. shows, the timers are very easy to use.  This method is superior
  28. for a large number of reasons:
  29.  
  30. ° The timer chip loop is more accurate.
  31.  
  32. ° The software loop method fails to produce accurate timing under
  33. a large number of circumstances.  The speed depends on what CPU
  34. is installed in the system, what video mode is selected, what
  35. type of memory the program is in, in what relation to vertical
  36. blank the code executes in, what the blitter is doing, what
  37. interrupts are enabled and more other factors than you want to
  38. think about.
  39.  
  40. ° The timer chip can be "set and forgotten".  The chip does the
  41. counting.  Your code can continue to get other things done while
  42. the chip is counting.
  43.  
  44. ° The timer can produce an interrupt when it is finished, or it
  45. can set a bit you can examine at any time.
  46.  
  47. ° The timer can be set to automatically reload the count and
  48. start again.  This gives even pulses, even if your software can't
  49. respond to them immediately.
  50.  
  51.  
  52. Calculating the time
  53.  
  54. First some definitions:
  55.  
  56.     1 milisecond  (ms)  = 1/1,000 second
  57.     1 microsecond (us)  = 1/1,000,000 second
  58.     1 nanosecond  (ns)  = 1/1,000,000,000 second
  59.  
  60. On a stock 68000 based Amiga with no extra memory, the "DBRA"
  61. instruction listed above will take about 1.5 microseconds per loop.  The loop above will thus waste about (2000 * 1.5 = 3000)
  62. microseconds (This is the same as 3 miliseconds, or .003
  63. seconds).
  64.  
  65. Each 8520 chip has two 16 bit timers counting down at .715909
  66. Mhz, or 1.3968255 microseconds per tick.  To get the same 3
  67. milisecond delay with the 8520, we need to divide the desired
  68. time by the rate.  3000 / 1.3968255 = 2148.
  69.  
  70.  
  71. A Complete Example
  72.  
  73.  
  74. ;
  75. ; A complete 8520 timing example.  This blinks the power light at
  76. ; (exactly) 3 milisecond intervals.  It takes over the machine,
  77. ; so watch out!
  78. ;
  79. ;
  80. ; The base Amiga crytal frequecies are:
  81. ;        NTSC    28.318181 Mhz
  82. ;        PAL     28.37516  Mhz
  83. ;
  84. ;
  85. ;
  86. ; The two 16 bit timers on the 8520 chips each count down at 1/10
  87. ; the CPU clock, or .715909 Mhz.  That works out to 1.3968255
  88. ; microseconds per count.  Under PAL the countdown is a hair
  89. ; slower, .709379 Mhz.
  90. ;
  91. ; To wait 1/100 second would require waiting 10,000 microseconds.
  92. ; The timer register would be set to (10,000 / 1.3968255 =
  93. ; 7159).
  94. ;
  95. ; To wait 3 miliseconds would require waiting 3000 microsecsonds.
  96. ; The register would be set to (3000 / 1.3968255 = 2148).
  97. ;
  98. ; See the hardware manual for more information on the 8520 chips.
  99. ;
  100. ciaatalo    EQU $bfe401    ;Timer A low
  101. ciaatahi    EQU $bfe501    ;Timer A high
  102. ciaaicr     EQU $bfed01    ;Interrupt control register
  103. ciaacra     EQU $bfee01    ;Timer A control
  104.  
  105.         move.w    #$7fff,$dff09a        ;Kill all custom chip interrupts
  106.  
  107. ;----Setup, only do once
  108. ;----This sets timer A to one-shot mode.
  109.         move.b    ciaacra,d0        ;Set control register A on CIAA
  110.         and.b    #%11000000,d0        ;Don't trash the 60/50Hz flag
  111.         or.b    #%00001000,d0        ;or serial direction bits
  112.         move.b    d0,ciaacra
  113.         move.b    #%01111111,ciaaicr  ;Clear all 8520 interrupts
  114.  
  115. ;----Set time (low byte THEN high byte)
  116. ;----And the low order with $ff
  117. ;----Shift the high order by 8
  118.         move.b    #(2148&255),ciaatalo
  119.         move.b    #(2148>>8),ciaatahi
  120.  
  121. ;----Wait for the timer to count down
  122. busy_wait:    btst.b    #0,ciaaicr        ;Wait for timer expired flag
  123.         beq.s    busy_wait
  124.         bchg.b    #1,$bfe001        ;Blink light
  125.         bset.b    #0,ciaacra        ;Restart timer
  126.         bra.s    busy_wait
  127.  
  128.          END
  129.